/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.wind.wallsopenings;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.hazards.wind.WindConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.WallsAndOpenings;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class WallsAndOpeningsForm extends AbstractForm implements BuildingUpdate {

	private Label cladding;
	private Label maximumExposure;
	private Label percentageDoorWindowOpenings;
	private Label glassType;
	private Label glassPaneConstruction;
	private Label shutterType;
	private Label shutterCompliance;
	private Label percentageOtherDoorArea;
	private Label garageDoors;
	private Label rollUpDoors;
	private Label doorProtectionType;
	private Label doorProtectionCompliance;
	
	// form fields
	private WindWallCladdingListBox formCladding = new WindWallCladdingListBox();
	private TextBox formMaximumExposure = new TextBox();
	private WindPercentageGlassDoorWindowOpeningListBox formPercentageDoorWindowOpenings = new WindPercentageGlassDoorWindowOpeningListBox();
	private WindGlassTypeListBox formGlassType = new WindGlassTypeListBox();
	private WindGlassPaneListBox formGlassPaneConstruction = new WindGlassPaneListBox();
	private WindDoorProtectionShutterListBox formShutterType = new WindDoorProtectionShutterListBox();
	private WindShutterCodeDoorProtectionListBox formShutterCompliance = new WindShutterCodeDoorProtectionListBox();
	private WindDoorAreaListBox formPercentageOtherDoorArea = new WindDoorAreaListBox();
	private WindGenericDoorsListBox formGarageDoors = new WindGenericDoorsListBox();
	private WindGenericDoorsListBox formRollUpDoors = new WindGenericDoorsListBox();
	private WindDoorProtectionShutterListBox formDoorProtectionType = new WindDoorProtectionShutterListBox();
	private WindShutterCodeDoorProtectionListBox formDoorProtectionCompliance = new WindShutterCodeDoorProtectionListBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(12,2);
	
	// entities
	private WallsAndOpenings wallsAndOpenings;
	
	// listeners
	private WindWallsOpeningsListBoxChangeListener windWallsOpeningsListBoxChangeListener;
	private WindWallsOpeningsTextBoxKeyListener windWallsOpeningsTextBoxKeyListener;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;
	
	private WindConstants constants;
	
	public WallsAndOpeningsForm(Building building) {

		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (WindConstants)GWT.create(WindConstants.class);
		tabTitle = constants.windWallsAndOpeningsTabTitle();
		
		cladding = new Label(constants.windWallsAndOpeningsCladding());
		maximumExposure = new Label(constants.windWallsAndOpeningsMaximumExposure());
		percentageDoorWindowOpenings = new Label(constants.windWallsAndOpeningsPercentageDoorWindowOpenings());
		glassType = new Label(constants.windWallsAndOpeningsGlassType());
		glassPaneConstruction = new Label(constants.windWallsAndOpeningsGlassPaneConstruction());
		shutterType = new Label(constants.windWallsAndOpeningsShutterType());
		shutterCompliance = new Label(constants.windWallsAndOpeningsShutterCompliance());
		percentageOtherDoorArea = new Label(constants.windWallsAndOpeningsPercentageOtherDoorArea());
		garageDoors = new Label(constants.windWallsAndOpeningsGarageDoors());
		rollUpDoors = new Label(constants.windWallsAndOpeningsRollUpDoors());
		doorProtectionType = new Label(constants.windWallsAndOpeningsDoorProtectionType());
		doorProtectionCompliance = new Label(constants.windWallsAndOpeningsDoorProtectionCompliance());
		
		// add labels to form
		table.setWidget(0, 0, cladding);
		table.setWidget(1, 0, maximumExposure);
		table.setWidget(2, 0, percentageDoorWindowOpenings);
		table.setWidget(3, 0, glassType);
		table.setWidget(4, 0, glassPaneConstruction);
		table.setWidget(5, 0, shutterType);
		table.setWidget(6, 0, shutterCompliance);
		table.setWidget(7, 0, percentageOtherDoorArea);
		table.setWidget(8, 0, garageDoors);
		table.setWidget(9, 0, rollUpDoors);
		table.setWidget(10, 0, doorProtectionType);
		table.setWidget(11, 0, doorProtectionCompliance);

		// handle entity
		wallsAndOpenings = building.getWallsAndOpenings();
		windWallsOpeningsListBoxChangeListener = new WindWallsOpeningsListBoxChangeListener(wallsAndOpenings);
		windWallsOpeningsTextBoxKeyListener = new WindWallsOpeningsTextBoxKeyListener(wallsAndOpenings);
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formCladding);
		table.setWidget(1, 1, formMaximumExposure);
		table.setWidget(2, 1, formPercentageDoorWindowOpenings);
		table.setWidget(3, 1, formGlassType);
		table.setWidget(4, 1, formGlassPaneConstruction);
		table.setWidget(5, 1, formShutterType);
		table.setWidget(6, 1, formShutterCompliance);
		table.setWidget(7, 1, formPercentageOtherDoorArea);
		table.setWidget(8, 1, formGarageDoors);
		table.setWidget(9, 1, formRollUpDoors);
		table.setWidget(10, 1, formDoorProtectionType);
		table.setWidget(11, 1, formDoorProtectionCompliance);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);	
		
	}

	public void updateBuilding(Building building) {
		
		// entities
		setBuilding(building);
		this.wallsAndOpenings = building.getWallsAndOpenings();
		
		// listeners
		windWallsOpeningsListBoxChangeListener.updateWallsAndOpenings(wallsAndOpenings);
		windWallsOpeningsTextBoxKeyListener.updateWallsAndOpenings(wallsAndOpenings);
		
		// reload forms
		loadForms();
		
	}

	private void loadForms()
	{
		
		if( wallsAndOpenings != null)
		{
			formCladding.setListItem( wallsAndOpenings.getCladding());
			formCladding.setName(WallsAndOpenings.CLADDING);
			
			String maximumExposure = "";
			if( wallsAndOpenings.getMaximumExposure() != null )
			{
				maximumExposure = wallsAndOpenings.getMaximumExposure().toString();
			}
			formMaximumExposure.setText( maximumExposure );
			formMaximumExposure.setName(WallsAndOpenings.MAXIMUMEXPOSURE);
			
			formPercentageDoorWindowOpenings.setListItem( wallsAndOpenings.getPercentageDoorWindowOpenings());
			formPercentageDoorWindowOpenings.setName(WallsAndOpenings.PERCENTAGEDOORWINDOWOPENINGS);
			
			formGlassType.setListItem( wallsAndOpenings.getGlassType());
			formGlassType.setName(WallsAndOpenings.GLASSTYPE);
			
			formGlassPaneConstruction.setListItem( wallsAndOpenings.getGlassPaneConstruction());
			formGlassPaneConstruction.setName(WallsAndOpenings.GLASSPANECONSTRUCTION);
			
			formShutterType.setListItem( wallsAndOpenings.getShutterType());
			formShutterType.setName(WallsAndOpenings.SHUTTERTYPE);
			
			formShutterCompliance.setListItem( wallsAndOpenings.getShutterCompliance());
			formShutterCompliance.setName(WallsAndOpenings.SHUTTERCOMPLIANCE);
			
			formPercentageOtherDoorArea.setListItem( wallsAndOpenings.getPercentageOtherDoorArea());
			formPercentageOtherDoorArea.setName(WallsAndOpenings.PERCENTAGEOTHERDOORAREA);
			
			formGarageDoors.setListItem( wallsAndOpenings.getGarageDoors());
			formGarageDoors.setName(WallsAndOpenings.GARAGEDOORS);
			
			formRollUpDoors.setListItem( wallsAndOpenings.getRollUpDoors());
			formRollUpDoors.setName(WallsAndOpenings.ROLLUPDOORS);
			
			formDoorProtectionType.setListItem( wallsAndOpenings.getDoorProtectionType());
			formDoorProtectionType.setName(WallsAndOpenings.DOORPROTECTIONTYPE);
			
			formDoorProtectionCompliance.setListItem( wallsAndOpenings.getDoorProtectionCompliance());
			formDoorProtectionCompliance.setName(WallsAndOpenings.DOORPROTECTIONCOMPLIANCE);
		}
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formCladding.setEnabled(false);
	            formMaximumExposure.setEnabled(false);
	            formPercentageDoorWindowOpenings.setEnabled(false);
	            formGlassType.setEnabled(false);
	            formGlassPaneConstruction.setEnabled(false);
	            formShutterType.setEnabled(false);
	            formShutterCompliance.setEnabled(false);
	            formPercentageOtherDoorArea.setEnabled(false);
	            formGarageDoors.setEnabled(false);
	            formRollUpDoors.setEnabled(false);
	            formDoorProtectionType.setEnabled(false);
	            formDoorProtectionCompliance.setEnabled(false);
	        }
	        else
	        {
	            formCladding.setEnabled(true);
	            formMaximumExposure.setEnabled(true);
	            formPercentageDoorWindowOpenings.setEnabled(true);
	            formGlassType.setEnabled(true);
	            formGlassPaneConstruction.setEnabled(true);
	            formShutterType.setEnabled(true);
	            formShutterCompliance.setEnabled(true);
	            formPercentageOtherDoorArea.setEnabled(true);
	            formGarageDoors.setEnabled(true);
	            formRollUpDoors.setEnabled(true);
	            formDoorProtectionType.setEnabled(true);
	            formDoorProtectionCompliance.setEnabled(true);            
	        }
		}
	}
	
	private void loadListeners()
	{
		formCladding.addChangeListener(windWallsOpeningsListBoxChangeListener);
		
		formMaximumExposure.addFocusListener(windWallsOpeningsTextBoxKeyListener);
		formMaximumExposure.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formPercentageDoorWindowOpenings.addChangeListener(windWallsOpeningsListBoxChangeListener);
		formGlassType.addChangeListener(windWallsOpeningsListBoxChangeListener);
		formGlassPaneConstruction.addChangeListener(windWallsOpeningsListBoxChangeListener);
		formShutterType.addChangeListener(windWallsOpeningsListBoxChangeListener);
		formShutterCompliance.addChangeListener(windWallsOpeningsListBoxChangeListener);
		formPercentageOtherDoorArea.addChangeListener(windWallsOpeningsListBoxChangeListener);
		formGarageDoors.addChangeListener(windWallsOpeningsListBoxChangeListener);
		formRollUpDoors.addChangeListener(windWallsOpeningsListBoxChangeListener);
		formDoorProtectionType.addChangeListener(windWallsOpeningsListBoxChangeListener);
		formDoorProtectionCompliance.addChangeListener(windWallsOpeningsListBoxChangeListener);
	}
}
